package com.cq.jackmar.rxdemo.demo;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Toast;

import com.cq.jackmar.rxdemo.R;
import com.cq.jackmar.rxdemo.base.BaseActivity;
import com.cq.jackmar.rxdemo.view.rvlist.RefreshUtil;
import com.cq.jackmar.rxdemo.view.imagepicker.ImagePickerUtils;
import com.cq.jackmar.rxdemo.view.rvlist.view.JRecyclerView;
import com.lzy.imagepicker.ImagePicker;
import com.lzy.imagepicker.bean.ImageItem;
import com.lzy.imagepicker.ui.ImageGridActivity;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.MultiItemTypeAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by JackMar on 2017/3/1.
 * 邮箱：1261404794@qq.com
 */

public class HomeActivity extends BaseActivity {
    @Bind(R.id.recyclerView)
    JRecyclerView mRecyclerView;
    private CommonAdapter<String> mAdapter;
    private RefreshUtil refreshUtil;
    private List<String> strings = new ArrayList<>();

    @Override
    protected int getLayoutId() {
        return R.layout.activity_list;
    }

    @Override
    protected void initView() {
        refreshUtil = new RefreshUtil(context, mRecyclerView);
        refreshUtil.setDefaultDivider();
        refreshUtil.enableLoad(false);
        refreshUtil.enableRefresh(false);
        mAdapter = new CommonAdapter<String>(context, R.layout.cell_list, strings) {
            @Override
            protected void convert(ViewHolder holder, String s, int position) {
                //绑定数据
                holder.setText(R.id.tv_text, s);
            }
        };
        mRecyclerView.setAdapter(mAdapter);
        mAdapter.setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
                switch (position) {
                    case 0:
                        launch(SingleRecyclerViewActivity.class);
                        break;
                    case 1:
                        launch(PtrRecyclerViewActivity.class);
                        break;
                    case 2:
                        launch(ProgressActivity.class);
                        break;
                    case 3:
                        ImagePickerUtils.setSelectCount(3);
                        Intent intent = new Intent(context, ImageGridActivity.class);
                        startActivityForResult(intent, 100);
                        break;
                }
            }

            @Override
            public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
                return false;
            }
        });
    }

    @Override
    protected void initData() {
        addData();
    }

    private void addData() {
        strings.add("使用recyclerview刷新加载");
        strings.add("ptr+recyclerView刷新加载");
        strings.add("显示dialog刷新");
        strings.add("开启图片选择");
        mAdapter.notifyDataSetChanged();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == ImagePicker.RESULT_CODE_ITEMS) {
            if (data != null && requestCode == 100) {
                ArrayList<ImageItem> images = (ArrayList<ImageItem>) data.getSerializableExtra(ImagePicker.EXTRA_RESULT_ITEMS);
            } else {
                Toast.makeText(this, "没有数据", Toast.LENGTH_SHORT).show();
            }
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
